import axios from 'axios'
import socket from '@/plugins/socket'
import Message from 'ant-design-vue/lib/message'
// import cookie from '@/plugins/cookie'
import $i18n from '@/plugins/i18n'
import store from '../store'
const messages = {
  zh: require('@/assets/languages/zh.json'),
  en: require('@/assets/languages/en.json')
}
let language = $i18n.locale
let axiosInstance = axios.create({
  // headers: { 'gorm.tenantId': '1', 'X-ACCUNT': 'jiashaohui@tupu360.com', 'X-UID': '531a244a5c9a4454015c9afc819e0001', 'X-EID': '1' },
  timeout: 60000,
  withCredentials: true
})
// 添加请求拦截器
axiosInstance.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  language = $i18n.locale
  const token = store.state.common.loginInfo && store.state.common.loginInfo.token
  if (token) {
    config.headers.Authorization = token
    if (config.headers.unAuthorization) {
      delete config.headers.unAuthorization
    }
  }
  return config
}, function (error) {
  // 对请求错误做些什
  return Promise.reject(error)
})

// 添加响应拦截器
axiosInstance.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return new Promise((resolve, reject) => {
    if (response.data.code === 206) {
      let timer
      let sok = socket.socketClient.subscribe(`/queue/operationNotification/${response.data.data.callbackId}`, tick => {
        console.log(tick)
        if (JSON.parse(tick.body).success === true) {
          timer && clearTimeout(timer)
          timer = null
          sok.unsubscribe()
          resolve({ data: { code: 200, message: messages[language].message.success } })
        }
      })
      timer = setTimeout(() => {
        sok.unsubscribe()
        resolve({ data: { code: 200, message: messages[language].message.success } })
      }, 10000)
    } else {
      resolve(response)
    }
    // resolve(response)
  })
}, function (error) {
  if (error.code === 'ECONNABORTED') {
    Message.error(messages[language].message.timeout)
    return Promise.reject(new Error({ message: messages[language].message.timeout, code: error.code }))
  }
  if (error.response.status === 401) { // 登录超时
    window.location.href = `${store.state.common.localPath}/vendors#/user/login`
    return Promise.reject(error)
  }
  if (error.response.status === 403) {
    // const origin = window.location.origin
    // const pathname = window.location.pathname
    // if (path.indexOf('interviewFeedBack') > 0) {
    //   return Promise.resolve({ data: { code: 403, message: '没有权限' } })
    // }
    // debounce(Message.error, messages[language].message.timeoutLoginAgain, 500)
    // Message.error('登录超时，请重新登录系统！')
    window.location = `${store.state.common.localPath}/vendors#/user/login`
    return Promise.reject(error)
  }
  if (error.response.status === 500) {
    Message.error(messages[language].message.internetDisconnected)
    return Promise.reject(new Error({ message: messages[language].message.internetDisconnected, code: error.response.status }))
  }
  // 操作候选人 已经有其他人操作
  if (error.response.status === 409 && error.response.data.code === 1112) {
    return Promise.resolve({ data: { code: error.response.status, message: error.response.data.message } })
  }
  Message.error('This is a message of error ' + error.response.status)
  return Promise.reject(error)
})

export default axiosInstance
